package ar.uba.fi.taller2.ftrs.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.prefs.Preferences;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import ar.uba.fi.taller2.ftrs.tree.BSTree;
import javax.swing.JTree;
import javax.swing.*;

@SuppressWarnings("serial")
public class Ventana extends JFrame
{
	
	/**
	 * Utilizado para setear preferencias
	 */
	private static Preferences prefs = Preferences.userNodeForPackage(Ventana.class);
	
	/**
	 * Solapas de la ventana
	 */
	private JTabbedPane tabbedPane;
	
	/**
	 * Panel de Mantenimiento de Documentos
	 */
	private JPanel panelDocumentos;
	
	/**
	 * Panel de Indice
	 */
	private JPanel panelIndice;
	/**
	 * Panel del Indice
	 */
	private JPanel panelConsultas;
	
		
	private AbstractAction accionBuscar;
	private AbstractAction accionAgregar;
	private AbstractAction accionQuitar;
	private AbstractAction accionIndexar;
	private AbstractAction accionAcercaDe;
	
	private JTextField jTextFieldBuscar;

	private GridBagConstraints c;

	private JTable tableResultadosBusqueda;
	private JTable tableDocumentos;

	private JMenuBar jMenuBar;
	private JMenu jMenuHelp;
	private BSTree arbolSim;
	private JTree repo;
	
		
	/**
	 * Funcin principal
	 * @param args
	 */
	public static void main(String[] args)
	{
		try
		{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e)
		{
			System.out.println("error");
		}
		new Ventana();
	}

	/**
	 * Constructor de clase que despliega al final la ventana.
	 */
	public Ventana()
	{
		super("Full Text Retrieval System");
		tabbedPane= new JTabbedPane();		
		InitAcciones();
		
		//Inicializa los contenidos de los paneles
		InitDocumentosTab();
		InitIndiceTab();
		InitConsultasTab();
		
		//Inserta los paneles en los tabs
		tabbedPane.addTab("Consultas", null, panelConsultas, "Consultas");
		tabbedPane.addTab("Documentos", null, panelDocumentos, "Documentos");
		tabbedPane.addTab("Indice", null, panelIndice, "Indice");
		add(tabbedPane, java.awt.BorderLayout.CENTER);
		
		//Setea el men superior de la ventana.
		jMenuBar = new JMenuBar();
		jMenuHelp = new JMenu("Ayuda");
		jMenuBar.add(jMenuHelp);
		JMenuItem jMenuItemAcercaDe= new JMenuItem(accionAcercaDe);
		jMenuHelp.add(jMenuItemAcercaDe);
		
		setJMenuBar(jMenuBar);
		
		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	private void InitAcciones()
	{
		accionQuitar = new AbstractAction("Quitar") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Quitar elemento");
			}
			public void actionPerformed(ActionEvent e)
			{
				Object[] options = {"Si", "No"};
				int n = JOptionPane.showOptionDialog(Ventana.this,
						"Est seguro que desea borrar los registros seleccionados?",
						"Atencin",
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.WARNING_MESSAGE,
						null,
						options,
						options[1]);
				
				/*if(n==0)
				{
					try {
						urls.borrar(Ventana.this.tableDocumentos.getSelectedRows());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}*/
									
			}
		};
		
		/*accionBuscar = new AbstractAction("Buscar") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Buscar pginas");
			}
			public void actionPerformed(ActionEvent e)
			{
				resultados.clearUrls();
				String cadenaBuscada;
				// TODO HAY QUE PONER AC LA LLAMADA A BUSCAR!				
				cadenaBuscada= jTextFieldBuscar.getText();
				Texto cadenaBuscada_input = new Texto();
				try {
				cadenaBuscada_input.parsear("spanish",cadenaBuscada);				
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					System.out.print("Error Parseando texto");
					e1.printStackTrace();
				}
				int[] cl=cadenaBuscada_input.buscar();
				
				for (int j=0; j< cl.length ;j++){
					//Hay que llamar a este mtodo para que se muestren los resultados					
					if (cl[j]!= -1) resultados.obtenerUrls(j);					
				}				
			}
		};*/
		
		accionAgregar = new AbstractAction("Agregar URL...") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Agregar URL");
			}
			public void actionPerformed(ActionEvent e)
			{
				DialogoAgregar dialogoAgregar = new DialogoAgregar(Ventana.this, "URL");
				dialogoAgregar.setVisible(true);
			}
		};
		
		accionIndexar = new AbstractAction("Indexar") {
			{
				putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Entrenar la SOM");
			}
			public void actionPerformed(ActionEvent e)
			{
				//TODO HAY QUE PONER AC LA LLAMADA A Entrenar
				
				/*/SOM= new neuralNetSom(5,4);
				SOM.setFile( SOM_File_Path);
				SOM.entrenar();				
				SOM.procesarRootwords();
				SOM.procesarUrls();
				
				//Toma los nuevos valores de roots de todas las urls
				try {
					urls.actualizar();
					resultados.actualizar();
				} catch (Exception e1) {
					e1.printStackTrace();
				}*/
			}
		};
		
		accionAcercaDe = new AbstractAction("Acerca de...") {
			{
				//putValue(AbstractAction.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
				putValue(AbstractAction.SHORT_DESCRIPTION, "Acerca de...");
			}
			public void actionPerformed(ActionEvent e)
			{
				JOptionPane.showMessageDialog(Ventana.this,
				 "Full Text Retrieval System v1.0\n\n\n" +
				 "Desarrolladores:\n" +
				 "XXXXX\n" +
				 "Perez, Juan Pablo\n" +
				 "XXXX\n" +
				 "XXXX\n" +
				 "Universidad de Buenos Aires\n" +
				 "Facultad de Ingeniera",
				 "Acerca de...",
				 JOptionPane.PLAIN_MESSAGE);					
			}
		};
	}
	
	private void InitDocumentosTab()
	{
		panelDocumentos= new JPanel();
		panelDocumentos.setLayout(new GridBagLayout());
		
		c = new GridBagConstraints();
		
		JPanel jPanelSuperior = new JPanel();
		jPanelSuperior.setLayout(new GridBagLayout());
		JPanel jPanelCentral = new JPanel();
		jPanelCentral.setLayout(new GridBagLayout());
		JPanel jPanelInferior = new JPanel();
		jPanelInferior.setLayout(new GridBagLayout());
		
		//Inserta los JPanels (superior, central e inferior) en el JPanel "panelEntrenamiento"
		//Se setea la parte superior
		c.gridx=0;
		c.gridy=0;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(0, 0, 0, 0);
		panelDocumentos.add(jPanelSuperior, c);
		
		//Se inserta el panel central
		c.gridx=0;
		c.gridy=1;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 0;
		c.insets = new Insets(0, 0, 0, 0);
		panelDocumentos.add(jPanelCentral, c);
		
		//Se inserta el panel inferior
		c.gridx=0;
		c.gridy=2;
		c.fill=GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.EAST;
		c.weightx = 1;
		c.weighty = 0;
		panelDocumentos.add(jPanelInferior, c);
		
		//Inserta componentes en la parte superior
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 3, 6);
		jPanelSuperior.add(new JLabel("Valores de entrenamiento:"), c);
		
		/*urls= new ModelUrls(dbcon);
		try {
			urls.llenar();
		} catch (SQLException e) {
			e.printStackTrace();
		}*/
		tableDocumentos = new JTable();
		c.fill=GridBagConstraints.BOTH;
		c.gridy = 1;
		c.insets = new Insets(3, 6, 6, 6);
		c.weightx = 1;
		c.weighty = 1;
		jPanelSuperior.add(new JScrollPane(tableDocumentos), c);
		
		//Inserta componentes en la parte central
		JButton botonAgregar = new JButton(accionAgregar);
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelCentral.add(botonAgregar, c);
		
		JButton botonQuitar = new JButton(accionQuitar);
		c.gridx = 1;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelCentral.add(botonQuitar, c);
		
		//Inserta componentes en la parte inferior
		JButton botonIndexar = new JButton(accionIndexar);
		c = new GridBagConstraints();
		c.insets = new Insets(6, 6, 6, 6);
		jPanelInferior.add(botonIndexar, c);
	}
	
	private void InitConsultasTab()
	{
		panelConsultas= new JPanel();
		panelConsultas.setLayout(new GridBagLayout());
		
		c = new GridBagConstraints();
		
		JPanel jPanelSuperior = new JPanel();
		jPanelSuperior.setLayout(new GridBagLayout());
		JPanel jPanelCentral = new JPanel();
		jPanelCentral.setLayout(new GridBagLayout());
		JPanel jPanelInferior = new JPanel();
		jPanelInferior.setLayout(new GridBagLayout());
		
		//Inserta los JPanels (superior, central e inferior) en el JPanel "panelConsultas"
		//Se setea la parte superior
		c.gridx=0;
		c.gridy=0;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		//c.weighty = 1;
		c.insets = new Insets(0, 0, 0, 0);
		panelConsultas.add(jPanelSuperior, c);
		
		//Se inserta el panel central
		c.gridx=0;
		c.gridy=1;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(0, 0, 0, 0);
		panelConsultas.add(jPanelCentral, c);
		
		//Se inserta el panel inferior
		c.gridx=0;
		c.gridy=2;
		c.anchor=GridBagConstraints.CENTER;
		c.weightx = 1;
		c.weighty = 0;
		panelConsultas.add(jPanelInferior, c);
		
		//Inserta componentes en la parte superior
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(new JLabel("Buscar:"), c);
		
		jTextFieldBuscar= new JTextField(60);
		c.gridx = 1;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(jTextFieldBuscar, c);
		
		JButton botonBuscar = new JButton(accionBuscar);
		c.gridx = 2;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(botonBuscar, c);
		
		
		//Inserta componentes en la parte superior
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelCentral.add(new JLabel("Resultado de la bsqueda:"), c);
		
		/*resultados= new ModelResultados(dbcon);
		try {
			resultados.llenar();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		tableResultadosBusqueda = new JTable(resultados);*/
		c.gridy = 1;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelCentral.add(new JScrollPane(tableResultadosBusqueda), c);
	}
	
	private void InitIndiceTab()
	{
		panelIndice= new JPanel();
		panelIndice.setLayout(new GridBagLayout());
		
		c = new GridBagConstraints();
		
		JPanel jPanelSuperior = new JPanel();
		jPanelSuperior.setLayout(new GridBagLayout());
		JPanel jPanelCentral = new JPanel();
		jPanelCentral.setLayout(new GridBagLayout());
		JPanel jPanelInferior = new JPanel();
		jPanelInferior.setLayout(new GridBagLayout());
		
		//Inserta los JPanels (superior, central e inferior) en el JPanel "panelConsultas"
		//Se setea la parte superior
		c.gridx=0;
		c.gridy=0;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		//c.weighty = 1;
		c.insets = new Insets(0, 0, 0, 0);
		panelIndice.add(jPanelSuperior, c);
		
		//Se inserta el panel central
		c.gridx=0;
		c.gridy=1;
		c.fill=GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(0, 0, 0, 0);
		panelIndice.add(jPanelCentral, c);
		
		//Se inserta el panel inferior
		c.gridx=0;
		c.gridy=2;
		c.anchor=GridBagConstraints.CENTER;
		c.weightx = 1;
		c.weighty = 0;
		panelIndice.add(jPanelInferior, c);
		
		//Inserta componentes en la parte superior
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(new JLabel("Buscar Termino:"), c);
		
		jTextFieldBuscar= new JTextField(60);
		c.gridx = 1;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(jTextFieldBuscar, c);
		
		JButton botonBuscar = new JButton(accionBuscar);
		c.gridx = 2;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);
		jPanelSuperior.add(botonBuscar, c);
		
		
		//Inserta componentes en la parte superior
		c = new GridBagConstraints();
		c.fill=GridBagConstraints.BOTH;
		/*c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		c.weighty = 0;
		c.insets = new Insets(6, 6, 6, 6);*/
		this.repo = new JTree();
		jPanelSuperior.add(this.repo, c);
		//this.repo.
		/*resultados= new ModelResultados(dbcon);
		try {
			resultados.llenar();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		tableResultadosBusqueda = new JTable(resultados);*/
		/*c.gridy = 1;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(6, 6, 6, 6);
		arbolSim = new BPTree();
		arbolSim.init();
		arbolSim.start();
		jPanelCentral.add(arbolSim, c);*/
	}
	
}